package com.example.spring_boo_test1.fileHeaders;

import sun.misc.BASE64Decoder;

import java.io.*;

import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;

public class FileHeaderUtils {
    public static void main(String[] args) throws Exception {
        String filePath = "C:\\MyFiles\\testFile\\冰冰科技8823合同.pdf"; // 指定要查看的文件路径
        String base64Str = getBase64(filePath);
//        System.out.println("base64=" + base64Str);
        String fileHeaderStr1 = getFileHeaderBase64(base64Str);
//        System.out.println(fileHeaderStr1);
//        String fileMime = getFileMimeStr(base64Str);
//        System.out.println(fileMime);



//        String fileHeaderStr = getFileHeaderStr(new File(filePath));
//        System.out.println(fileHeaderStr);
    }

    public static String getFileMimeStr(String base64Str) throws Exception {
        byte[] decodedBytes = Base64.getDecoder().decode(base64Str);

        URL url = new URL("data:application/octet-stream;base64," + Base64.getEncoder().encodeToString(decodedBytes));
        URLConnection connection = url.openConnection();
        return connection.getContentType();
    }

    public static String getFileHeaderBase64(String base64Str) throws Exception {
        try {
            byte[] decodedBytes = Base64.getDecoder().decode(base64Str);

            // 获取文件头的前几个字节（通常是文件头的长度）
            int headerLength = 8; // 根据实际情况确定文件头的长度
            byte[] fileHeader = new byte[headerLength];
            System.arraycopy(decodedBytes, 0, fileHeader, 0, headerLength);

            // 将文件头的字节数组转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : fileHeader) {
                hexString.append(String.format("%02x", b));
            }

            System.out.println("文件头的十六进制编码: " + hexString.toString());
            return hexString.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "获取文件头编码失败";
    }

    public static String getFileHeaderStr(File file) throws Exception {
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] headerBytes = new byte[8]; // 设置需要读取的字节数（通常为4或8）

            int bytesRead = fis.read(headerBytes); // 从输入流中读取字节到byte数组

            if (bytesRead == -1 || bytesRead < headerBytes.length) {
                System.out.println("无法读取文件头");
            } else {
                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < headerBytes.length; i++) {
                    sb.append(Integer.toString((headerBytes[i] & 0xff) + 0x100, 16).substring(1));
                }

                System.out.println("文件头标识：" + sb.toString());
                return sb.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "获取文件头失败";
    }

    public static String getBase64(String path) throws Exception {
        Path filePath = Paths.get(path);
        byte[] fileBytes = Files.readAllBytes(filePath);
        String base64EncodedString = Base64.getEncoder().encodeToString(fileBytes);
        return base64EncodedString;
    }
}
